package com.siriframe.security.expression;

import com.siriframe.security.entity.po.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("zhku")
public class ZhkuExpression {

    public boolean hasAuthority(String authority){
        //获取当前用户的权限
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        List<String> permissions = loginUser.getPermissions();
        //判断用户权限集合是否存在authority
        return permissions.contains(authority);
    }

    public boolean hasAnyAuthority(String...authoritys){
        //获取当前用户的权限
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) authentication.getPrincipal();
        List<String> permissions = loginUser.getPermissions();
        //判断用户权限集合是否存在authority
        for(String authority : authoritys){
            if(permissions.contains(authority)){
                return true;
            }
        }
        return false;
    }
}
